#include "String.h"

class Solution {
public:
    string addStrings(string num1, string num2) {
        string s;
        int end1 = num1.size() - 1, end2 = num2.size() - 1;
        s.reserve(max(num1.size(), num2.size()) + 1);
        int x = 0;
        while (end1 >= 0 || end2 >= 0)
        {
            int x1 = end1 >= 0 ? num1[end1--] - '0' : 0;
            int x2 = end2 >= 0 ? num2[end2--] - '0' : 0;
            x = x1 + x2 + x;
            //s.insert(s.begin(), x % 10 + '0');
            s += ('0' + x % 10);
            x /= 10;
        }
        //if (x == 1) s.insert(s.begin(), '1');
        if (x == 1) s += '1';
        reverse(s.begin(), s.end());
        return s;
    }
};